/*
 * Copyright (C) 2014 Tolstykh Vladislav
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package lib.search;

import com.google.common.collect.TreeMultimap;
import java.util.Map;
import lib.scripture.Chapter;
import lib.scripture.Content;
import lib.scripture.HashableBook;
import lib.scripture.Title;
import lib.scripture.Verse;
import lib.sort.Sort;
import lib.sort.Sortable;

/**
 * Обработчик.
 * @author Толстых Владислав
 */
public class Handler {
    private final Content CONTENT;
    private final Title TITLE;
    
    /**
     * Выполняем поиск в одной книге. 
     * @param book где будем искать
     */
    public Handler(HashableBook book) {
        CONTENT = book.getContent();
        TITLE=book.getTitle();
    }
    
    /**
     * Собираем из вектора мультимэпу для данной главы.
     * @param residue вектор результатов
     * @return результаты поиска ввиде "Описание":"Стих"
     */
    public TreeMultimap<Description, Verse> collect(TreeMultimap<Chapter,Verse> residue) {        
        TreeMultimap<Description, Verse> collect = TreeMultimap.create(
            new Sortable(Sort.AS_IS), new Sortable(Sort.VERSE)
        );
        for (Map.Entry<Chapter, Verse> entrySet : residue.entries()) {
            Chapter chapter = entrySet.getKey();
            Verse verse = entrySet.getValue();
            collect.put(new Description(TITLE, chapter), verse);            
        }
        return collect;
    }
    
    /**
     * Содержимое.
     * @return главы книги
     */
    public Content сontent() {
        return CONTENT;
    }
}
